#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
账本 - 余额分账功能使用示例
"""

from examples.base_example import BaseExample
from jlpay.services.ledger.balancesplit_service import BalanceSplitService
from jlpay.model.ledger.balancesplit import BalanceSplitRequest


class BalanceSplitExample(BaseExample):
    """余额分账示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.ledger.balancesplit.split")
        self.service = BalanceSplitService(self.config)
    
    def build_balance_split_request(self):
        """构建余额分账请求"""
        request = BalanceSplitRequest()
        
        # 设置请求参数
        request.set_org_code("50720711")
        request.set_mch_id("849581183510017")
        request.set_out_order_no("045e1314f7784f46bf9d590dc65c7")
        request.set_total_amount(1000)
        
        # 设置分账接收方列表
        receivers = [
            {
                "rev_mch_id": "849584307420004",
                "amount": 500,
                "description": "分账给接收方1"
            },
            {
                "rev_mch_id": "849602058120001",
                "amount": 500,
                "description": "分账给接收方2"
            }
        ]
        request.set_receivers(receivers)
        
        return request
    
    def demo(self):
        """执行余额分账示例"""
        self.logger.info("开始执行余额分账示例")
        
        # 创建余额分账请求
        request = self.build_balance_split_request()
        
        # 发送请求
        response = self.send_request_and_handle_response(
            self.service.split_balance,
            "余额分账",
            request
        )
        
        # 处理响应结果
        if response and response.is_success():
            self.logger.info(f"响应报文：{response.to_dict()}")


if __name__ == "__main__":
    example = BalanceSplitExample()
    example.logger.info("=" * 50)
    example.logger.info("===== 余额分账示例 开始 =====")
    example.logger.info("=" * 50)
    
    example.demo()
    
    example.logger.info("=" * 50)
    example.logger.info("===== 余额分账示例执行完毕 =====")
    example.logger.info("=" * 50) 